package it.appcatalogobase;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import it.appcatalogobase.R;
import it.appcatalogobase.R.id;
import it.appcatalogobase.R.layout;
import it.appcatalogobase.R.menu;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DataAppPersistence appData = (DataAppPersistence)getApplication();
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
		}
		Button b = (Button) findViewById(R.id.button1);
		 if(appData.isSync()){
			 b.setEnabled(true);
		 }else{
			 ConnectivityManager conMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
			 NetworkInfo intC = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			 
			 if(intC.isConnected()){
				 MyAsyncTask task = new MyAsyncTask("www.tempogara.it/arcobaleno/temp/catalogo.php?data=categorie", this);
				 task.execute();			 
				 b.setEnabled(false);
				 appData.setSync(true);
			 }
		 }
		 
		
		 
//		 TextView txt = (TextView) findViewById(R.id.textView1);
//		 AssetManager assetManager = getAssets();
//	        InputStream inputStream=null;
//	        Properties properties=null;
//			try {
//				inputStream = assetManager.open("config.properties");
//				properties = new Properties();
//				properties.load(inputStream);
//			} catch (IOException e5) {
//				// TODO Auto-generated catch block
//				e5.printStackTrace();
//			}
//			
//		 txt.setText(Html.fromHtml("<p style='text-align: center;'><span style='font-size: 12pt; color: #ff0000;'><strong>Frog Revolution </strong></span></p>\r\n<p><span style='font-size: 10pt;'>Un mix perfetto di design e praticit&agrave;, che stimola il piacere dello sguardo.</span></p>\r\n<p><span style='font-size: 10pt;'>La Frog &egrave; una macchina da caff&egrave; espresso a cialde destinata sia al mercato domestico che quello aziendale. </span></p>\r\n<p><span style='font-size: 10pt;'>Il suo design innovativo, la sua semplicit&agrave; e la qualit&agrave; delle sue componenti, la rendono un bene strumentale affidabile a cui puntare in tutta tranquillit&agrave;. </span></p>\r\n<p><span style='font-size: 10pt;'><strong>DIMENSIONI:</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Larghezza: 22 cm </span></li>\r\n<li><span style='font-size: 10pt;'>Altezza: 40 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Profondit&agrave;: 32 cm</span></li>\r\n<li><span style='font-size: 10pt;'>Peso: 6.5 Kg</span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>CARATTERISTICHE TECNICHE</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Potenza normale 650 W </span></li>\r\n<li><span style='font-size: 10pt;'>Voltaggio 230 V</span></li>\r\n<li><span style='font-size: 10pt;'>Sistema di decalcificazione rapido </span></li>\r\n<li><span style='font-size: 10pt;'>Alloggio bottiglia d'acqua </span></li>\r\n</ul>\r\n<p><span style='font-size: 10pt;'><strong>ACCESSORI</strong></span></p>\r\n<ul>\r\n<li><span style='font-size: 10pt;'>Vano porta cialde e bicchieri</span></li>\r\n</ul>"),TextView.BufferType.SPANNABLE);
			 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
		return super.onOptionsItemSelected(item);
	}
	
	public void listCategorie(View v){
		Intent i = new Intent(MainActivity.this, CategoryListActivity.class);
		startActivity(i);
	}
	
	public void sync(View v){
		MyAsyncTask task = new MyAsyncTask("www.tempogara.it/arcobaleno/temp/catalogo.php?data=categorie", this);
		task.execute();
	}
	
	public void listAllProducts(View v){
		Intent i = new Intent(MainActivity.this, ListProductsActivity.class);
		i.putExtra("type", "all");
		startActivity(i);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}
